<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="author" content="wangbo">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>DOM</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .box {
      margin: 200px auto;
      display: flex;
      justify-content: space-around;
      text-align: center;
      width: 600px;
      height: 112px;
    }
    .box div{flex: 1;}
  </style>
</head>

<body>
  <div class="box">
    <div style="background-color: red;">div1</div>
    <div style="background-color: greenyellow;">div2</div>
    <div style="background-color: hotpink;">div3</div>
    <div style="background-color:aqua;">div4</div>
  </div>
  <script>
    var divArr = document.querySelector('.box').querySelectorAll('div');
    var body = document.querySelector('body');
    for (let i = 0; i < divArr.length; i++) {
      divArr[i].onclick = function () {
        // 别忘了获取body元素的方法，document.body
        body.style.backgroundColor = divArr[i].style.backgroundColor
      }
    }
  </script>
</body>

</html>